﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.Exceptions;

public partial class Templates_HomePage_Default : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        valEmailFormat.ValidationExpression = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
        ClientScript.RegisterStartupScript(this.GetType(), "attachAccordianExpanded", "Sys.Application.add_load(attachAccordianExpanded);", true);
        if (!IsPostBack)
        {
            //Force the first accordian panel to open with javascript
            accordian.RequireOpenedPane = false;
            ClientScript.RegisterStartupScript(this.GetType(), "expandInitial", "Sys.Application.add_load(expandInitial);", true);
        }
        else
        {
            accordian.RequireOpenedPane = true;
        }
    }

    protected void dsNewsletter_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        Post newsletter = Post.GetBySiteIdAndPath(AcmsContext.SiteId, "~/Newsletters/", AcmsContext.SiteVersion);
        if (newsletter != null && newsletter.Children.Count > 0)
        {
            e.SetData(newsletter.Children[0].Children);
        }
    }

    protected void dsCountries_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        Post countries = Post.GetBySiteIdAndPath(AcmsContext.SiteId, "~/Countries/", AcmsContext.SiteVersion);
        if (countries != null)
        {
            e.SetData(countries.Children);
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            WorldClassData.WorldClassDataContext context = new WorldClassData.WorldClassDataContext();
            WorldClassData.NewsletterSignup registration = new WorldClassData.NewsletterSignup();
            registration.Name = txtName.Text;
            registration.EmailAddress = txtEmail.Text;
            registration.Job = txtJob.Text;
            registration.Bar = txtBar.Text;
            registration.City = txtCity.Text;
            registration.Country = txtCountry.Text;

            context.NewsletterSignups.InsertOnSubmit(registration);
            try
            {
                context.SubmitChanges();
                Email tmp = Email.CreateInstance(registration.Name, registration.EmailAddress, Server.MapPath("~/Emails/NewsletterEmail.xml"));
                tmp.Format("Name", registration.Name);
                tmp.Send();
            }
            catch(ValidationException ex)
            {
                foreach (ValidationExceptionItem item in ex.Items)
                {
                    Page.Trace.Warn(item.Message);
                }
                dlNewsletterExecption.DataSource = ex.Items;
                dlNewsletterExecption.DataBind();
                pnlNewsletterExecption.Visible = true;
                return;
            }

            pnlRegister.Visible = false;
            pnlRegistered.Visible = true;

        }


    }

    protected void dlCountries_SelectedIndexChanged(object sender, EventArgs e)
    {
        int id = 0;
        try
        {
            id = int.Parse(dlCountries.SelectedValue);
        }
        catch
        {
        }

        Post country = Post.GetById(id, AcmsContext.SiteVersion);
        if(country != null)
        {
            dlFAQ.SelectedIndex = 0;
            faqSelected.Visible = regCompSelected.Visible = true;
            dlFAQ.DataBind();

            //lnkTC.NavigateUrl = country.GetUrl("PopUp");
        }
        else
        {
            faqSelected.Visible = regCompSelected.Visible = false;
        }
    }

    protected void dsFaqs_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        int id = 0;
        try
        {
            id = int.Parse(dlCountries.SelectedValue);
        }
        catch
        {
        }

        Post country = Post.GetById(id, AcmsContext.SiteVersion);
        if (country != null)
        {
            e.SetData(country.Children);
        }

    }


    protected void btnCompRegister_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Post country = Post.GetById(int.Parse(dlCountries.SelectedValue), AcmsContext.SiteVersion);
            if(country != null)
            {
                PostControl emailAddressControl = country.FindPostControl("EmailAddress");
                if(emailAddressControl != null)
                {
                    Acms.AcmsControls.TextContentControlProperties emailAddressProperties = emailAddressControl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
                    if(emailAddressProperties != null)
                    {
                        Registration.WorldClassDataContext context = new Registration.WorldClassDataContext();
                        Registration.WorldClassSignup registration = new Registration.WorldClassSignup();
                        registration.Name = txtCompName.Text;
                        registration.EmailAddress = txtCompEmail.Text;
                        registration.Job = txtCompJob.Text;
                        registration.Bar = txtCompBar.Text;
                        registration.City = txtCompCity.Text;
                        registration.Country = txtCompCountry.Text;
                        registration.MainCountry = country.DisplayName;

                        context.WorldClassSignups.InsertOnSubmit(registration);
                        try
                        {
                            context.SubmitChanges();
                            Email tmp = Email.CreateInstance(registration.Name, registration.EmailAddress, Server.MapPath("~/Emails/WorldClassEmail.xml"));
                            tmp.Format("Name", registration.Name);
                            tmp.Format("MainCountry", registration.MainCountry);
                            tmp.Send();
                            Email coordinatorEmail = Email.CreateInstance("Coordinator", emailAddressProperties.Text.Trim(), Server.MapPath("~/Emails/WorldClassCoordinatorEmail.xml"));
                            coordinatorEmail.Format("Name", registration.Name);
                            coordinatorEmail.Format("EmailAddress", registration.EmailAddress);
                            coordinatorEmail.Format("Job", registration.Job);
                            coordinatorEmail.Format("Bar", registration.Bar);
                            coordinatorEmail.Format("City", registration.City);
                            coordinatorEmail.Format("Country", registration.Country);
                            coordinatorEmail.Format("Created", registration.Created.ToString("dd MMMM yyyy HH:mm"));
                            coordinatorEmail.Send();
                        }
                        catch (ValidationException ex)
                        {
                            foreach (ValidationExceptionItem item in ex.Items)
                            {
                                Page.Trace.Warn(item.Message);
                            }
                            dlCompExecption.DataSource = ex.Items;
                            dlCompExecption.DataBind();
                            pnlCompExecption.Visible = true;
                            return;
                        }

                        pnlRegisterComp.Visible = false;
                        pnlRegisteredComp.Visible = true;
                    }
                }
            }
        }
    }

    protected void dlFAQ_ItemDataBound(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    {
    }
}
